package com.lwq.weatherapp.slice;

import com.lwq.weatherapp.ResourceTable;
import com.lwq.weatherapp.data.CityMo;
import com.lwq.weatherapp.data.ListItemProvider;
import com.lwq.weatherapp.net.HiNet;
import com.lwq.weatherapp.net.IHiNet;
import com.lwq.weatherapp.util.HiExecutor;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Image;
import ohos.agp.components.ListContainer;
import ohos.agp.components.Text;
import org.devio.hi.json.HiJson;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class MainAbilitySlice extends AbilitySlice implements ListItemProvider.OnItemClickListener {

     private ListContainer listContainer;
     private ListItemProvider listItemProvider;
     private ArrayList<CityMo> cities = new ArrayList<CityMo>();
     private HiNet hiNet;
     private Text temperatureText,weatherText,tipsText;
     private Image weatherImage;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main);
        hiNet = new HiNet();
        initLayout();
        initCity();
        loadData(cities.get(0));
    }

    //初始化城市
    private void initCity() {
        cities.add(new CityMo("北京", "110000"));
        cities.add(new CityMo("上海", "310000"));
        cities.add(new CityMo("广州", "440100"));
        cities.add(new CityMo("深圳", "440300"));
        cities.add(new CityMo("重庆", "500000"));
        cities.add(new CityMo("杭州", "330100"));
        cities.add(new CityMo("成都", "510100"));
        cities.add(new CityMo("武汉", "420100"));
        cities.add(new CityMo("南京", "310100"));
        listItemProvider.setData(cities);
    }

    //初始化布局
    private void initLayout() {
        listContainer = (ListContainer) findComponentById(ResourceTable.Id_list);
        listItemProvider = new ListItemProvider(this, this);
        listContainer.setItemProvider(listItemProvider);
        //初始化text组件,用于后续网络请求后渲染UI
        temperatureText = (Text) findComponentById(ResourceTable.Id_temperature);
        weatherText = (Text) findComponentById(ResourceTable.Id_weather);
        tipsText = (Text) findComponentById(ResourceTable.Id_tips);
        weatherImage = (Image) findComponentById(ResourceTable.Id_weather_icon);

    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    /**
     *  点击item事件
     *  @param mo
     *  @param position
     */
    @Override
    public void onItemClick(CityMo mo, int position) {
        loadData(mo);
    }

    /**
     *  请求数据
     *  @param mo
     */
    private void loadData(CityMo mo) {
        Map<String, String> params = new HashMap<>();
        params.put("city", mo.cityCode);
        params.put("key", "b5a8727156352c8024b3e284a3c3b5df");
        hiNet.get("https://restapi.amap.com/v3/weather/weatherInfo", params, new IHiNet.NetListener() {
            @Override
            public void onSuccess(HiJson res) {
                System.out.println(res);
                bindData(res,mo);
            }

            @Override
            public void onFail(String message) {
                System.out.println(message);
            }
        });
    }

    /**
     *  绑定数据
     *  @param res
     *  @param mo
     */
    private void bindData(HiJson res, CityMo mo) {
//        HiJson hiJson = new HiJson(jsonString).get("lives").get(0);
        //这里需要改成接口获取的动态数据
        HiJson hiJson = res.get("lives").get(0);
        String province = hiJson.value("province");
        String temperature = hiJson.value("temperature");//温度
        String weather = hiJson.value("weather");//天气
        String winddirection = hiJson.value("winddirection");//风向
        String windpower = hiJson.value("windpower");//风级
        String humidity = hiJson.value("humidity");//空气湿度
        temperatureText.setText(temperature);
        weatherText.setText(weather);
        tipsText.setText(mo.cityName + ": " + winddirection + "风" + windpower + " 空气湿度" + humidity + "%");
        int id = ResourceTable.Media_sunshine;
        if (weather.contains("阴")) {
            id = ResourceTable.Media_overcast;
        }else if (weather.contains("雨")) {
            id = ResourceTable.Media_rain;
        }
        //设置id
        weatherImage.setImageAndDecodeBounds(id);

    }

    String jsonString = "{\n" +
            "    \"status\": \"1\",\n" +
            "    \"count\": \"1\",\n" +
            "    \"info\": \"OK\",\n" +
            "    \"infocode\": \"10000\",\n" +
            "    \"lives\": [\n" +
            "        {\n" +
            "            \"province\": \"河北\",\n" +
            "            \"city\": \"邢台市\",\n" +
            "            \"adcode\": \"130500\",\n" +
            "            \"weather\": \"多云\",\n" +
            "            \"temperature\": \"17\",\n" +
            "            \"winddirection\": \"北\",\n" +
            "            \"windpower\": \"≤3\",\n" +
            "            \"humidity\": \"98\",\n" +
            "            \"reporttime\": \"2020-09-26 21:58:34\"\n" +
            "        }\n" +
            "    ]\n" +
            "}";
}
